(function(undefined){
	var city_obj = {};

	city_obj.config = {
		url: '/handle.php?h=city/area_list',
		title_pro: "请选择省份/城市",
		title_city: "请选择城市/区县",
		title_dis: "请选择地区",
		defaultpro: 0,
		defaultcity: 0,
		defaultdis: 0,
		rank: 2,
		spell:0
	}
	city_obj.obj = {
		province: null,
		city: null,
		district: null ,
		city_name: null
	}
	//初始化
	city_obj.init = function(param){
		for(i in param){
			city_obj.config[i] = param[i];
		}

		city_obj.obj.province = $("#city_pro");
		city_obj.obj.city = $("#city_city");
		city_obj.obj.city_name = $("#city_name");
		if(city_obj.config.rank == 3){
			city_obj.obj.dis = $("#city_dis");
		}


		city_obj.obj.province.bind("change", city_obj.pro_change);
		if (city_obj.config.rank == 3) {
			city_obj.obj.city.bind("change", city_obj.city_change);
		}

		city_obj.pro_init();    //初始化省份

	}

	//省份改变函数
	city_obj.pro_change = function(){
		var val = city_obj.obj.province.val();
		//去掉第三个的内容
		if (city_obj.config.rank == 3) {
			html_obj.form.create_option(city_obj.obj.dis, city_obj.config.title_dis, []);
		}
		if(!val){
			html_obj.form.create_option(city_obj.obj.city, city_obj.config.title_city, []);
		}else{
			city_obj.get_data(val,2, city_obj.set_city);
		}


	}


	city_obj.city_change = function(){
		var val = city_obj.obj.city.val();
		if(!val){
			html_obj.form.create_option(city_obj.obj.dis, city_obj.config.title_dis, []);
		}else{
			city_obj.get_data(val,3, city_obj.set_dis);
		}
	}


	//初始化省份
	city_obj.pro_init = function(){
		city_obj.get_data(0,1, city_obj.set_pro);
		//如果设置了城市的默认值,则要初始化城市
		if(city_obj.config.defaultpro){
			city_obj.get_data(city_obj.config.defaultpro,2, city_obj.set_city);
		}else{
			//没有设置默认城市，
			html_obj.form.create_option(city_obj.obj.city, city_obj.config.title_city);
		}
		if (city_obj.config.rank == 3) {
			if (city_obj.config.defaultpro && city_obj.config.defaultcity) {
				city_obj.get_data(city_obj.config.defaultcity,3, city_obj.set_dis);
			}
			else {
				html_obj.form.create_option(city_obj.obj.dis, city_obj.config.title_dis);
			}
		}



	}

	//设置城市数据
	city_obj.set_city = function(data){
		html_obj.form.create_option(city_obj.obj.city, city_obj.config.title_city, data, "id", "city_name", city_obj.config.defaultcity,"letter");

		if(city_obj.config.spell){
			html_obj.form.create_option(city_obj.obj.city, city_obj.config.title_city, data, "spelling", "city_name", city_obj.config.defaultcity,"letter");
		}

		html_obj.form.create_hidden(city_obj.obj.city_name, city_obj.config.title_city, data, "id", "city_name", city_obj.config.defaultcity,"letter");
		if(typeof(city_name_cus) !='undefind'){
			city_name_cus  = html_obj.form.create_hidden(city_obj.obj.city_name, city_obj.config.title_city, data, "id", "city_name", city_obj.config.defaultcity,"letter");
		}
	}

	//设置城市数据
	city_obj.set_pro = function(data){
		html_obj.form.create_option(city_obj.obj.province, city_obj.config.title_pro, data, "id", "city_name", city_obj.config.defaultpro,"letter");

	}

	//设置地区数据
	city_obj.set_dis = function(data){
		html_obj.form.create_option(city_obj.obj.dis, city_obj.config.title_dis, data, "id", "city_name", city_obj.config.defaultdis,"letter");
		if(city_obj.config.spell){
		html_obj.form.create_option(city_obj.obj.dis, city_obj.config.title_dis, data, "spelling", "city_name", city_obj.config.defaultdis,"letter");
		}
	}


	//得到数据
	/**
	* @param mixed id 城市id
	* @param mixed level 城市区域级别
	*/
	city_obj.get_data = function(id,level, handle){
		$.getJSON(city_obj.config.url, {'id':id,'level':level}, function(data){
			handle(data.msg);
		})
	}



	if(window.city_obj == undefined || window.city_obj == '未定义'){
		window.city_obj = city_obj;
	}
})();